package com.admin.boot.modules.online.controller;

import com.admin.boot.common.utils.Result;
import com.admin.boot.modules.online.entity.OnlineTable;
import com.admin.boot.modules.online.entity.OnlineTableField;
import com.admin.boot.modules.online.service.IOnlineTableFieldService;
import com.admin.boot.modules.online.service.IOnlineTableService;
import com.admin.boot.modules.online.vo.CodeGenerateVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @version v3.0
 * @Description: TODO
 * @author: jeecg-boot
 * @date: 2024-01-09
 */
@RestController
@RequestMapping("/onlineTable/onlineTable")
public class OnlineTableController {

    @Autowired
    private IOnlineTableService onlineTableService;

    @Autowired
    private IOnlineTableFieldService onlineTableFieldService;


    @GetMapping("/list")
    public Result<?> list(OnlineTable onlineTable,
                          @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                          @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        Page<OnlineTable> onlineTablePage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<OnlineTable> queryWrapper = new LambdaQueryWrapper<>();
        if (onlineTable.getTableName()!=null){
            queryWrapper.eq(OnlineTable::getTableName,onlineTable.getTableName());
        }
        Page<OnlineTable> page = onlineTableService.page(onlineTablePage, queryWrapper);
        for (OnlineTable record : page.getRecords()) {
            LambdaQueryWrapper<OnlineTableField> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OnlineTableField::getTableId,record.getId());
            List<OnlineTableField> list = onlineTableFieldService.list(wrapper);
            record.setTableFields(list);
        }
        return Result.OK(page);

    }

    @GetMapping("/getByTableName")

    public Result<?> list(String tableName){
        LambdaQueryWrapper<OnlineTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OnlineTable::getTableName,tableName);
        List<OnlineTable> list = onlineTableService.list(queryWrapper);
        return Result.OK(list);

    }


    @PostMapping("/edit")
    public Result<?> edit(@RequestBody OnlineTable onlineTable){
        return onlineTableService.edit(onlineTable);
    }

    @PostMapping("/add")
    public Result<?> addTable(@RequestBody OnlineTable onlineTable){
        return onlineTableService.addTable(onlineTable);
    }


    @GetMapping("/deleteById")
    public Result<?> deleteById(String id){
        return onlineTableService.deleteTable(id);
    }

    @GetMapping("/syncDb")
    public Result<?> syncDb(String id){
        return onlineTableService.syncDb(id);
    }


    @PostMapping("/codeGenerate")
    public Result<?> codeGenerate(@RequestBody CodeGenerateVo codeGenerateVo) throws TemplateException, IOException {
        onlineTableService.codeGenerate(codeGenerateVo);
        return Result.OK("");
    }
}
